package com.fruitshop.manager;

import com.fruitshop.manager.base.BaseManager;
import com.fruitshop.model.Command;
import com.fruitshop.util.UserInfoContext;

import java.util.ArrayList;
import java.util.List;

// 应用程序管理类
public class ApplicationManager extends BaseManager {


    // 用户注册
    private static final String USER_REGISTER = "1";

    // 用户登录
    private static final String USER_LOGIN = "2";

    // 水果管理
    private static final String FRUIT_MANAGE = "3";

    // 购买记录管理
    private static final String BUY_RECORD="4";

    // 管理员注册
    private static final String ADMIN_REGISTER = "5";

    // 管理员登录
    private static final String ADMIN_LOGIN = "6";

    public ApplicationManager(String title,List<Command> commandList) {
        super(title, commandList,true);
    }


    @Override
    protected String processCommand(String command) {

        switch (command) {
            case USER_REGISTER:
                return showUserRegister();

            case USER_LOGIN:
                return showUserLogin();
            case FRUIT_MANAGE:
                return showFruitManage();
            case BUY_RECORD:
                return showBuyRecord();
            case ADMIN_REGISTER:
                return showAdminRegister();
            case ADMIN_LOGIN:
                return showAdminLogin();

        }

        return command;
    }

    private String showAdminLogin() {
        ArrayList<Command> commandList = new ArrayList<>();
        commandList.add(new Command("1", "管理员登录",false));
        return new AdminLoginManager("管理员登录", commandList).run();
    }

    private String showAdminRegister() {
        ArrayList<Command> commandList = new ArrayList<>();
        commandList.add(new Command("1", "管理员注册",false));
        return new AdminRegisterManager("管理员注册", commandList).run();
    }


    @Override
    protected List<Command> filterShowCommandList(List<Command> showCommandList) {
        if(UserInfoContext.isLogin()){
            if(UserInfoContext.isAdmin()){
                showCommandList.removeIf(command -> command.getId().equals(ADMIN_LOGIN));
            }else{
                showCommandList.removeIf(command -> command.getId().equals(USER_LOGIN));
            }
        }
        return showCommandList;
    }


    private String showBuyRecord() {
        ArrayList<Command> commandList = new ArrayList<>();
        commandList.add(new Command("1", "个人购买记录",true,Command.SCOPE_USER));
        commandList.add(new Command("2", "全部购买记录",true));
        return new BuyRecordManager("购买记录", commandList).run();
    }

    private String showUserRegister() {
        ArrayList<Command> commandList = new ArrayList<>();
        commandList.add(new Command("1", "注册用户",false));
        return new UserRegisterManager("用户注册", commandList).run();
    }

    private String showUserLogin() {
        ArrayList<Command> commandList = new ArrayList<>();
        commandList.add(new Command("1", "登录用户",false));
        return new UserLoginManager("用户登录", commandList).run();
    }

    private String showFruitManage() {
        ArrayList<Command> commandList = new ArrayList<>();
        commandList.add(new Command("1", "水果列表",true,Command.SCOPE_ALL));
        commandList.add(new Command("2", "水果新增",true,Command.SCOPE_ADMIN));

        commandList.add(new Command("3", "水果删除",true,Command.SCOPE_ADMIN));
        commandList.add(new Command("4", "水果修改",true,Command.SCOPE_ADMIN));
        commandList.add(new Command("5", "水果购买",true,Command.SCOPE_USER));
        return new FruitManager("水果管理", commandList).run();
    }
}
